package com.charles.video.system.controller;

import com.charles.video.api.domain.SysUser;
import com.charles.video.common.core.controller.BaseController;
import com.charles.video.common.core.responseDomain.AjaxResult;
import com.charles.video.common.core.responseDomain.TableInfo;
import com.charles.video.common.log.annotation.Log;
import com.charles.video.system.mapper.SysUserMapper;
import com.charles.video.system.service.SysUserService;
import com.charles.video.system.vo.SearchUserForm;
import com.charles.video.system.vo.UserModifyForm;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Charles-H
 * 
 * 用户数据控制层
 */
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController {
    
    @Resource
    private SysUserMapper sysUserMapper;
    
    @Resource
    private SysUserService sysUserService;
    
    /**
     * 获取用户表格数据
     */
    @GetMapping("/getUserTableData")
    public TableInfo getUserTableData(SearchUserForm searchUserForm) {
        startPage();
        return getDataTable(sysUserMapper.queryUserTableData(searchUserForm));
    }

    /**
     * 通过用户ID获取用户详情
     */
    @GetMapping("/getUserDetail/{userId}")
    public AjaxResult getUserDetail(@PathVariable("userId") Long userId) {
        return AjaxResult.success(sysUserMapper.queryUserDetail(userId));
    }

    /**
     * 通过用户ID获取用户详情
     */
    @GetMapping("/getUserModifyForm/{userId}")
    public AjaxResult getUserModifyForm(@PathVariable("userId") Long userId) {
        return AjaxResult.success(sysUserMapper.queryUserModifyForm(userId));
    }

    /**
     * 新增用户
     */
    @Log(behavior = "新增了一名视频用户")
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody SysUser user) {
        SysUser sysUser = sysUserService.validateAddUser(user);
        return toAjax(sysUserMapper.insertUser(sysUser));
    }

    /**
     * 修改用户
     */
    @Log(behavior = "修改了一名视频用户信息")
    @PutMapping("/modifyUser")
    public AjaxResult modifyUser(@RequestBody UserModifyForm user) {
        UserModifyForm userModifyForm = sysUserService.validateModifyUser(user);
        return toAjax(sysUserMapper.modifyUser(userModifyForm));
    }

    /**
     * 删除用户
     */
    @Log(behavior = "删除了一名视频用户信息")
    @DeleteMapping("/deleteUser/{userId}")
    public AjaxResult deleteUser(@PathVariable("userId") Long userId) {
        return toAjax(sysUserMapper.deleteUser(userId));
    }

}
